package com.jams.goodox.activity.home;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.login.LoginActivity;
import com.jams.goodox.activity.qa.parent.TeacherListFragment;
import com.jams.goodox.listen.SuccessListener;
import com.jams.goodox.utils.StatusBarCompat;
import com.jams.goodox.utils.TipAlertDialog;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.UnScrollViewPager;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.OnClick;

public class HomeActivity extends BaseActivity  {

    private static final String TAG = HomeActivity.class.getSimpleName();
    public static final int TAB_MAIN = 0;//首页
    public static final int TAB_SOCIAL = 1;//圈子
    public static final int TAB_ENTERPRISE = 2;//企业
    public static final int TAB_MINE = 3;//我的
    @Bind(R.id.tab_main_img)
    ImageView tabMainImg;
    @Bind(R.id.tab_main)
    TextView tabMain;
    @Bind(R.id.tab_life_img)
    ImageView tabLifeImg;
    @Bind(R.id.tab_life)
    TextView tabLife;
    @Bind(R.id.tab_neighbour_img)
    ImageView tabNeighbourImg;
    @Bind(R.id.tab_enterprise)
    TextView tabNeighbour;
    @Bind(R.id.tab_mine_img)
    ImageView tabMineImg;
    @Bind(R.id.tab_mine)
    TextView tabMine;
    @Bind(R.id.red_point)
    ImageView redPoint;
    @Bind(R.id.bottom_navigation_bar)
    LinearLayout bottomNavigationBar;
    @Bind(R.id.vp_main_content)
    UnScrollViewPager mViewPager;

    private BaseFragment homeFragment;//首页
    private BaseFragment socialIntercourseFragment;//课堂页面
    private BaseFragment enterPriseFragment;//问答页面
    private BaseFragment mineFragment;//我的页面

    public final static String MODE = "mode";
    private int selectTab = TAB_MAIN;//当前选择的tab

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void init() {
        setTitleVisibility(false);//隐藏头
        StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.main_theme));//沉侵式头部
        homeFragment = HomeFragment2.newInstance("1");
        socialIntercourseFragment = new ClassRoomFragment();

//        if(2 == SePref.getMemberType()){
//            //身份是教师
//            enterPriseFragment = new TeacherQAFragment();
//            mineFragment = MineTeacherFragment.newInstance("1");
//        }else {
//            //身份是家长
//            enterPriseFragment = new TeacherListFragment();
//            mineFragment = MineFragment.newInstance("1");
//        }

        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return homeFragment;
                    case 1:
                        return socialIntercourseFragment;
                    case 2:
                        return enterPriseFragment;
                    case 3:
//                        if (!SePref.isLogin()){
//                            startActivity(new Intent(HomeActivity.this, LoginActivity.class));
//                        }
                        return mineFragment;
                    default:
                        return homeFragment;
                }
            }

            @Override
            public int getCount() {
                return 4;
            }
        };

        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setAdapter(fragmentPagerAdapter);
        onTabSelected(TAB_MAIN);


        NotificationManagerCompat manager = NotificationManagerCompat.from(this);
        boolean isOpened = manager.areNotificationsEnabled();
        if(!isOpened){
            TipAlertDialog.showTip(this, null, null,
                    "打开通知权限", new SuccessListener() {
                        @Override
                        public void success() {
                            // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
                            Intent intent = new Intent();
                            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package",
                                    getPackageName(), null);
                            intent.setData(uri);
                            startActivity(intent);
//                            startActivityForResult(intent,0x99);
                        }
                    });

        }

    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if(requestCode == 0x99){
//
//            NotificationManagerCompat manager = NotificationManagerCompat.from(this);
//            boolean isOpened = manager.areNotificationsEnabled();
//        }
//    }

    public void onTabSelected(int position) {
        switch (position) {
            case TAB_MAIN://首页
                //StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.blue));//沉侵式头部
                mViewPager.setCurrentItem(0, false);
                changeState(TAB_MAIN);
                break;
            case TAB_SOCIAL://
                //StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.blue));//沉侵式头部
                mViewPager.setCurrentItem(1, false);
                changeState(TAB_SOCIAL);
                break;
            case TAB_ENTERPRISE://
//                if (selectTab != TAB_ENTERPRISE && enterPriseFragment != null) {
//                    enterPriseFragment.getEnterpriseData();
//                }
                //StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.blue));//沉侵式头部
                mViewPager.setCurrentItem(2, false);
                changeState(TAB_ENTERPRISE);
                break;
            case TAB_MINE:
//                if (selectTab != TAB_MINE && mineFragment != null) {
//                    mineFragment.getUserInfo();
//                }
                if(!SePref.isLogin()){
                    startActivity(new Intent(HomeActivity.this,LoginActivity.class));
                    return;
                }
                //StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.blue));//沉侵式头部
                mViewPager.setCurrentItem(3, false);
                changeState(TAB_MINE);
                break;
            default:
                //StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.blue));//沉侵式头部
                mViewPager.setCurrentItem(0, false);
                changeState(TAB_MAIN);
                break;
        }
    }


    //切换选中状态
    private void changeState(int selectIndex) {
        selectTab = selectIndex;
        tabMainImg.setImageResource(selectIndex == TAB_MAIN ? R.drawable.tab_shouye_h : R.drawable.tab_shouye_n);
        tabLifeImg.setImageResource(selectIndex == TAB_SOCIAL ? R.drawable.tab_ketang_h : R.drawable.tab_ketang_n);
        tabNeighbourImg.setImageResource(selectIndex == TAB_ENTERPRISE ? R.drawable.tab_wenda_h : R.drawable.tab_wenda_n);
        tabMineImg.setImageResource(selectIndex == TAB_MINE ? R.drawable.tab_wode_h : R.drawable.tab_wode_n);

        tabMain.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_MAIN ? R.color.main_color : R.color.font_color_gray));
        tabLife.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_SOCIAL ? R.color.main_color : R.color.font_color_gray));
        tabNeighbour.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_ENTERPRISE ? R.color.main_color : R.color.font_color_gray));
        tabMine.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_MINE ? R.color.main_color : R.color.font_color_gray));


//        tabMainImg.setImageResource(selectIndex == TAB_MAIN ? R.drawable.tab_main_anim : R.mipmap.tab_home);
//        tabLifeImg.setImageResource(selectIndex == TAB_SOCIAL ? R.drawable.tab_life_anim : R.mipmap.tab_life);
//        tabNeighbourImg.setImageResource(selectIndex == TAB_ENTERPRISE ? R.drawable.tab_enterprise_anim : R.mipmap.tab_enterprise);
//        tabMineImg.setImageResource(selectIndex == TAB_MINE ? R.drawable.tab_mine_anim : R.mipmap.tab_mine);
//
//        tabMain.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_MAIN ? R.color.font_tab_default_color : R.color.font_color_gray));
//        tabLife.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_SOCIAL ? R.color.font_tab_default_color : R.color.font_color_gray));
//        tabNeighbour.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_ENTERPRISE ? R.color.font_tab_default_color : R.color.font_color_gray));
//        tabMine.setTextColor(ContextCompat.getColor(this, selectIndex == TAB_MINE ? R.color.font_tab_default_color : R.color.font_color_gray));

        /*AnimationDrawable animationDrawable;
        switch (selectIndex) {
            case TAB_MAIN:
                animationDrawable = (AnimationDrawable) tabMainImg.getDrawable();
                animationDrawable.start();
                break;
            case TAB_SOCIAL:
                animationDrawable = (AnimationDrawable) tabLifeImg.getDrawable();
                animationDrawable.start();
                break;
            case TAB_ENTERPRISE:
                animationDrawable = (AnimationDrawable) tabNeighbourImg.getDrawable();
                animationDrawable.start();
                break;
            case TAB_MINE:
                animationDrawable = (AnimationDrawable) tabMineImg.getDrawable();
                animationDrawable.start();
                break;
            default:
                animationDrawable = (AnimationDrawable) tabMainImg.getDrawable();
                animationDrawable.start();
                break;
        }*/
    }

    @OnClick({R.id.tab_main_btn, R.id.tab_life_btn, R.id.tab_enterprise_btn, R.id.tab_mine_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab_main_btn:
                onTabSelected(TAB_MAIN);
                break;
            case R.id.tab_life_btn:
                onTabSelected(TAB_SOCIAL);
                break;
            case R.id.tab_enterprise_btn:
                onTabSelected(TAB_ENTERPRISE);
                break;
            case R.id.tab_mine_btn:
                onTabSelected(TAB_MINE);
//                if(SePref.isLogin()){
//                    onTabSelected(TAB_MINE);
//                }else {
//                    startActivity(new Intent(HomeActivity.this,LoginActivity.class));
//                }
                break;
            default:
                onTabSelected(TAB_MAIN);
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit;
        if (!isExit) {
            isExit = true; // 准备退出
            ToastUtil.show(this,"再按一次退出");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG,"-----onresume-----" + SePref.getMemberType());
        if(2 == SePref.getMemberType()){
            //身份是教师
            enterPriseFragment = new TeacherQAFragment();
            mineFragment = MineTeacherFragment.newInstance("1");
        }else {
            //身份是家长
            enterPriseFragment = new TeacherListFragment();
            mineFragment = MineFragment.newInstance("1");
        }
    }

}
